package com.spukmk3me.io.userinput;

public abstract class ButtonInput
{
    protected ButtonInput()
    {
        m_behaviours = new int[ 64 ];
        m_freshPress = new boolean[ 64 ];
    }
    
    public abstract boolean hasTypedChar();
    public abstract char nextTypedChar();
    
    /**
     *  
     *  @param deltaTime 16-16 fixed number
     */
    public final void process( int deltaTime )
    {
        long scanFlag = 1;
        
        for ( int i = 0; i != 64; ++i )
        {
            int behaviour = m_behaviours[ i ];
            
            if ( (behaviour & BEHAVIOUR_ONETIMEPRESS) != 0 )
            {
                if ( !m_freshPress[ i ] )
                    m_pressData &= ~scanFlag;
            }
            
            scanFlag <<= 1;
        }
        
        for ( int i = 0; i != 64; ++i )
            m_freshPress[ i ] = false;
    }
    
    public final long getData()
    {
        return m_pressData;
    }
    
    public final void setBehaviour( long flag, int behaviour )
    {
        long scanFlag = 1;
        
        for ( int i = 0; i != 64; ++i )
        {
            if ( (scanFlag & flag) != 0 )
                m_behaviours[ i ] = behaviour;
            
            scanFlag <<= 1;
        }
    }
    
    protected final void applyPressData( long flag, boolean isPressed )
    {
        if ( flag == 0 )
            return;
        
        if ( isPressed )
            m_pressData |= flag;
        else
            m_pressData &= ~flag;
        
        long scanFlag = 1;
        
        for ( int i = 0; i != 64; ++i )
        {
            if ( (scanFlag & flag) != 0 )
                m_freshPress[ i ] = isPressed;
            
            scanFlag <<= 1;
        }
    }
    
    public static final int BEHAVIOUR_ONETIMEPRESS = 0x00000001;
    
    private int[]       m_behaviours;
    private boolean[]   m_freshPress;
    private long        m_pressData;
}
